#!/usr/bin/env ruby

# The HookCollection class is used to define and execute custom _hooks_, 
# i.e. portions of custom codes to be executed in certain key points of RedBook's core classes
# (normally used in plugins).
class RedBook::HookCollection < Hash
		
		# Execute code associated to the _index_ hook with _args_ (used internally inside core classes).
		def exec(index, args={})
			if self[index] then
				self[index].children.each do |h|
					return true if h.value.call(args) == true # If the hook returns true, stop executing hooks
				end
			end
			return false
		end
		
		# Create a new _index_ hook.
		# It must be used in a <tt>initialize_[plugin_name]</tt> method of a plugin, 
		# within the core class to extend (e.g. RedBook::Logger, RedBook::Parser).
		# _proc_ corresponds to the actual implementation of the hook.
		# 
		# <i>Example:</i>
		#		def initialize_my_plugin
		#			@hooks.add :after_start do
		#				# Your code here...
		#			end
		#		end
		def add(index, &hook_code)
			unless self[index]
				self[index] = {index => nil}
			end
			self[index] << {:hook => hook_code}
		end
	
end
